<?php
class Admin_penilaian extends CI_Controller {

	const VIEW_FOLDER = 'admin';
	
	public function __construct()
    {
        parent::__construct();
		$this->load->model('admin/model_admin_soal');
		$this->load->model('admin/model_admin');

		if($this->session->userdata('is_logged_in')=="user"){
			redirect('user/test');
        }else if(!$this->session->userdata('is_logged_in')){
			redirect('user/login');
        }
	}
	
	public function index()
    {
		$data['title']							= "Automatic Essay Scoring - Penilaian";
		$jenis_test_id						= $this->uri->segment(3);
		$method								= $this->input->post('method');
		$error_digit							= $this->input->post('error_digit');
		if($jenis_test_id !== false){
			$data['jenis_test_selected'] = $jenis_test_id;
			if($method){$data['method_selected'] = $method;}else{$data['method_selected'] ='bleu';}
			$data['error_selected']		= $this->error_validation($error_digit);
			$data['data']						= $this->model_admin_soal->get_soal_by_jenis_test($jenis_test_id);
			foreach($data['data'] as $id_soal){
				$data['kunci'][$id_soal['id']]		 = $this->model_admin_soal->get_kunci_by_id_soal($id_soal['id']);
				$data['jml_kunci'][$id_soal['id']] = $this->model_admin_soal->count_kunci_by_id_soal($id_soal['id']);
				$data['jawaban'][$id_soal['id']]	 = $this->model_admin_soal->get_jawaban_by_id_soal($id_soal['id']);
				$data['jml_jawaban'][$id_soal['id']]= $this->model_admin_soal->count_jawaban_by_id_soal($id_soal['id']);
				$data['skor'][$id_soal['id']]		 = null;
				$data['message'][$id_soal['id']] = null;
			}
			$data['jml']							= count($data['data']);
		}else{
			$data['jenis_test_selected'] = 0;
			$data['method_selected']	= 'bleu';
			$data['error_selected']		= $this->error_validation($error_digit);
			$data['data']						= null;
		}
		
		$data['user']	 	= $this->model_admin->get_user();
		
		if ($this->input->server('REQUEST_METHOD') === 'POST'){
			$this->form_validation->set_rules('no', 'no', 'required');
            $this->form_validation->set_rules('check', 'check', 'required');
            $this->form_validation->set_rules('method', 'method', 'required');
            $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">&times;</a><span class="text-danger">', '</span></div>');
			if ($this->form_validation->run()){
				$no			 = $this->input->post('no');
				$soal		 = $this->input->post('check');
				foreach($soal as $id_soal){
					$data['check'][$id_soal] = $no[$id_soal];
					if($data['jml_kunci'][$id_soal] != 0 and $data['jml_jawaban'][$id_soal]!=0){
						if($method == 'tfidf'){
							$data['title']				 = "Automatic Essay Scoring - Penilaian (TF-IDF)";
							$data['skor'][$id_soal]	 = $this->tfidf($data['kunci'][$id_soal], $data['jawaban'][$id_soal], $data['error_selected']);
						}else if($method == 'bleu'){
							$data['title']				 = "Automatic Essay Scoring - Penilaian (BLEU)";
							$data['skor'][$id_soal]	 = $this->bleu($data['kunci'][$id_soal], $data['jawaban'][$id_soal], $data['error_selected']);
						}
						$data['message'][$id_soal]		 = null;
					}else{
						$data['message'][$id_soal]		 = 'Please check anwers and keys';
					}
				}
				if($this->input->post('all')){
					$data['check']['all'] = true;
				}
				foreach($data['user'] as $id_user){
					$check = $this->model_admin->check_user($id_user['username']);
					if($check){
						$data['nilai'][$id_user['username']] = 0;
						foreach($data['data'] as $id_soal){
							if(isset($data['skor'][$id_soal['id']]['nilai']['nilai'][$id_user['username']])){
								$data['nilai'][$id_user['username']] = $data['nilai'][$id_user['username']] + $data['skor'][$id_soal['id']]['nilai']['nilai'][$id_user['username']];
							}else{
								$data['nilai'][$id_user['username']] = $data['nilai'][$id_user['username']] + 0;
							}
						}
					}else{
						$data['nilai'][$id_user['username']] = 0;
					}
				}
			}
		}
		$data['jenis_test']		 = $this->model_admin->get_jenis_test();
		$data['main_content']	 = 'admin/view_admin_penilaian';
		$this->load->view('includes/template_admin', $data);
	}
	
	//-------TFIDF METHOD START-------
	public function tfidf($kunci, $jawaban, $error_digit)
	{
		$data['ngram'] 				= $this->input->post('ngram');
		$data['kalimat'] 				= $this->string_validation($kunci, $jawaban);
		$data['hasil_tfidf_token'] 	= $this->tfidf_token($data['kalimat']);
		$data['ngram'] 				= $this->ngram_validation($data['ngram'], $data['hasil_tfidf_token']['ngram']);
		$data['hasil_proses'] 		= $this->tfidf_method($data['hasil_tfidf_token'], $data['ngram'], $error_digit);
		$data['nilai']					= $this->tfidf_normalisasi($data['hasil_proses']);
		
		return $data;
	}
	
	public function tfidf_normalisasi($hasil){
		if($hasil){
		$this->load->model('admin/model_fitur');
		$hasil['normal']	= $this->model_fitur->tfidf_normal($hasil);
		$hasil['nilai']		= $this->model_fitur->nilai($hasil);
		}
		return $hasil;
	}
	
	public function tfidf_token($kalimat){
		if($kalimat){
		$this->load->model('admin/model_fitur');
		
		$kunci 					= $this->model_fitur->tfidf_token_kunci($kalimat);
		$jawaban 				= $this->model_fitur->tfidf_token_jawaban($kalimat, $kunci['token']);
		$token['ngram']		= $this->token_ngram($kunci['ngram'], $jawaban['ngram']);
		$token['kunci']	 	= $kunci;
		$token['jawaban'] 	= $jawaban;
		$token['skor']			= $kalimat['skor'];
		
		return $token;
		}
	}
	
	public function token_ngram($kunci, $jawaban){
		if($kunci < $jawaban){$ngram = $kunci;}else{$ngram = $jawaban;}
		return $ngram;
	}
	
	public function error_validation($error){
		if($error!=0){$error = $error;}else{$error = 4;}
		return $error;
	}
	
	public function ngram_validation($ngram, $token_ngram){
		if($ngram<=$token_ngram && $ngram!=0){$ngram = $ngram;}else{$ngram = 1;}
		return $ngram;
	}
	
	public function tfidf_method($kalimat, $ngram, $error_digit){
		if($kalimat){
		$this->load->model('admin/model_fitur');
		$this->load->model('admin/model_tfidf');
		
		$hasil 					= $this->model_fitur->matching_tfidf($kalimat, $ngram);
		$hasil['error_digit'] 	= $error_digit;
		$hasil['df'] 				= $this->model_tfidf->df($hasil);
		$hasil['D/df'] 			= $this->model_tfidf->Ddf($hasil, $hasil['jumlah_kunci']); 
		$hasil['W'] 			= $this->model_tfidf->W($hasil);
		$hasil['bobot_W'] 		= $this->model_tfidf->bobot_W($hasil);
		$hasil['VSM'] 			= $this->model_tfidf->VSM($hasil);
		$hasil['kkdoc'] 		= $this->model_tfidf->kkdoc($hasil);
		$hasil['sqrt'] 			= $this->model_tfidf->sqrt($hasil);
		$hasil['cosin'] 		= $this->model_tfidf->cosin($hasil);
		$hasil['skor']			= $kalimat['skor'];
		
		return $hasil;
		}
	}
	//-------TFIDF METHOD END-------
	
	
	//-------BLUE METHOD START-------
	public function bleu($kunci, $jawaban, $error_digit){
		$data['kalimat'] 				= $this->string_validation($kunci, $jawaban);
		$data['hasil_tfidf_token'] 	= $this->bleu_token($data['kalimat']);
		$data['hasil_proses']		= $this->bleu_method($data['hasil_tfidf_token'], $error_digit);
		$data['nilai']					= $this->bleu_normalisasi($data['hasil_proses']);
		
		return $data;
	}
	
	public function bleu_normalisasi($hasil){
		if($hasil){
		$this->load->model('admin/model_fitur');
		$hasil['normal']	= $this->model_fitur->bleu_normal($hasil);
		$hasil['nilai']		= $this->model_fitur->nilai($hasil);
		}
		return $hasil;
	}
	
	public function bleu_token($kalimat){
		if($kalimat){
		$this->load->model('admin/model_fitur');
		$hasil = $this->model_fitur->bleu_token_kalimat($kalimat);
		return $hasil;
		}
	}
	
	public function bleu_method($kalimat, $error_digit){
		if($kalimat){
		$kalimat['error_digit'] 	= $error_digit;
		$kalimat['e']				= 2.71828;
		$kalimat['skor']			= $kalimat['skor'];
		
		$this->load->model('admin/model_fitur');
		$this->load->model('admin/model_bleu');
		
		$hasil = $this->model_fitur->matching_bleu($kalimat);
		$hasil = $this->model_bleu->hitung_bleu($hasil);
		
		return $hasil;
		}
	}
	
	//-------------STRING VALIDATION--------------//
	public function string_validation($kunci, $jawaban)
	{
		$hasil = array();
			foreach($jawaban as $value){
				$hasil_jawaban_siswa[$value['id_user']] 	= $value['jawaban'];
				$check_jawaban[$value['id_user']] 			= $value['id_user'];
			}
			foreach($kunci as $value){
				$hasil_kunci[$value['id']] 	= $value['kunci'];
				$check_kunci[$value['id']] 	= $value['id'];
				$skor[$value['id']]			= $value['skor'];
			}
			$hasil['kunci'] 		= $hasil_kunci;
			$hasil['jawaban_siswa'] = $hasil_jawaban_siswa;
			$hasil['check_jawaban'] = $check_jawaban;
			$hasil['check_kunci'] 	= $check_kunci;
			$hasil['skor']			= $skor;
		
		return $hasil;
	}
	
	//-------------GRAFIK--------------//
	public function grafik(){
		$data['title']				 = "Automatic Essay Scoring - Grafik";
		$data['main_content']	 = 'admin/view_admin_grafik';
		$this->load->view('includes/template_admin', $data);
	}
	
}